#include <REGX52.H>
#include "KeyTimer0.h"

unsigned char KeyNum;


//----------------------------------
//对外获取按键值函数
unsigned char GetKey_Num(){
	unsigned char t;
	t = KeyNum;   //返回值
	KeyNum = 0;   //每次清零
	return t;
}


unsigned char GetKeyNum()//获取键码函数(内部调用)
{
	unsigned char Key=0;
	if(P3_1==0) Key=1;//按下Key代表键码,松手是Key = 0
	if(P3_0==0) Key=2;
	if(P3_2==0) Key=3;
	if(P3_3==0) Key=4;

	return Key;//返回键码数值
}


void KeyScan(){
	static unsigned char nowKey,lastKey,K1count,K2count,K3count,K4count; 
	lastKey = nowKey;
	
	
	nowKey = GetKeyNum();//获取按键值
	
	//按下&&抬起 有时间差10ms扫描，直到松手
	
	if(lastKey == 1 && nowKey == 1){K1count++;}
	if(lastKey == 2 && nowKey == 2){K2count++;}
	if(lastKey == 3 && nowKey == 3){K3count++;}
	if(lastKey == 4 && nowKey == 4){K4count++;}
	
	if(K1count>=10 && K1count<=100 && nowKey==0) {KeyNum = 1;K1count=0;}  //100ms-1s,短按
	else if(K1count>100 && nowKey==0) {KeyNum = 11;K1count=0;}			//大于1s,长按
	if(K2count>=10 && K2count<=100 && nowKey==0) {KeyNum = 2;K2count=0;}
	else if(K2count>100 && nowKey==0) {KeyNum = 22;K2count=0;}			
	if(K3count>=10 && K3count<=100 && nowKey==0) {KeyNum = 3;K3count=0;}
	else if(K3count>100 && nowKey==0) {KeyNum = 33;K3count=0;}
	
	if(K4count>=10 && K4count<=100 && nowKey==0) {KeyNum = 4;K4count=0;}
	else if(K4count>100 && nowKey==0) {KeyNum = 44;K4count=0;}
//	if(K4count>=10 && K4count<=100 && nowKey==0) {KeyNum = 4;K4count=0;}
//	else if(K4count>100 && nowKey==0) {KeyNum = 44;}K4count=0;
		
}

